#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2017/4/23 18:00
# @Author  : chen
# @Site    : 
# @File    : Util.py
# @Software: PyCharm
from datetime import datetime, timedelta


def time_transfer(str_time):
    # print str_time
    now_time = datetime.now()
    if not str_time:
        return ""
    # 今天
    if str_time.find("\u4eca\u5929") != -1:

        return now_time.strftime("%Y-%m-%d")
    # 小时前
    elif str_time.find("\u5c0f\u65f6\u524d") != -1:
        hour_ps = str_time.find("\u5c0f\u65f6\u524d")
        hour = int(str_time[0:hour_ps])
        return (now_time - timedelta(hours=hour)).strftime("%Y-%m-%d-%H") + "-00-00"
    #天前
    elif str_time.find("\u5929\u524d") != -1:
        day_ps = str_time.find("\u5929\u524d")
        day = int(str_time[0:day_ps])
        return (now_time - timedelta(days=day)).strftime("%Y-%m-%d")
    elif str_time.find("-") != -1:
    # -
        return str_time
    # 分钟前
    elif str_time.find("\u5206\u949f\u524d") != -1:
        min_ps = str_time.find("\u5206\u949f\u524d")
        min = int(str_time[0:min_ps])
        return (now_time - timedelta(minutes=min)).strftime("%Y-%m-%d-%H-%M") + "-00"
    else:
        return now_time.strftime("%Y-%m-%d")
        
if __name__ == '__main__':
    print time_transfer("5天前")
    print time_transfer("今天")
    print time_transfer("5小时前")
    print time_transfer("5\u5206\u949f\u524d")
